package com.spring.cloud.consume.service.impl;


import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.spring.cloud.consume.service.ConsumeService;
import com.spring.cloud.consume.service.FeignProvideService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Service
public class ConsumeServiceImpl implements ConsumeService {
    public Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    RestTemplate restTemplate;
    @Resource
    FeignProvideService feignProvideService;

    @Override
    public String ribbon(String name) {
        return restTemplate.getForObject("http://service-provide/provide/ribbon?name="+name,String.class);
    }

    @Override
    public String feign(String name) {
        return feignProvideService.feign(name);
    }

    /**
     *   restTemplate 熔断
     * @param name
     * @return
     */
    @Override
    @HystrixCommand(fallbackMethod = "turbineErrorBack")
    public String hystrix(String name) {
        log.info("call provide turbineError......");
        return restTemplate.getForObject("http://service-provide/provide/hystrix?name="+name,String.class);
    }

    private String turbineErrorBack(String name) {
        log.info("hystrix   test from restTemplate turbineErrorBack......");
        return "turbineErroFallBackr:"+name;
    }


}
